package top.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.pojo.*;
import top.component.ExampleCase;

import javax.sql.DataSource;
import java.util.Properties;

@Data
public class ReadConfig {

    @Autowired
    private ExampleCase exampleCase;

    private static ClassPathXmlApplicationContext context;

    static {
        context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
    }

    public static void close(ClassPathXmlApplicationContext context){
        context.close();
    }


    public static void main(String[] args) {
        DataSource dataSource = getDataSource();
        System.out.println(dataSource);
        getSource();
        getFather();
        ReadConfig readConfig = new ReadConfig();
        System.out.println(readConfig.getExampleCase());
    }


    public static void getFather(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("top");
        Father father = (Father) context.getBean("father");
        System.out.println(father);
    }


    public static void getSource(){
        Source source = (Source) context.getBean("source");
        System.out.println(source);
    }

    public static void JNDI(){
        DruidDataSource dataSource = (DruidDataSource) context.getBean("dataSource");
        System.out.println(dataSource);
    }


    public static void constructorDI(){
        ConstructorBean constructorBean = (ConstructorBean) context.getBean("constructorBean");
        System.out.println(constructorBean);
    }

    public static void initMaket(){
        Maket maket = (Maket) context.getBean("maket");
        System.out.println(maket);
    }

    public static void initMaketTwo(){
        MaketTwo maketTwo = (MaketTwo) context.getBean("maketTwo");
        System.out.println(maketTwo);
        System.out.println(maketTwo.makeMaketTwo());

        System.out.println("======================================================");

        Maket maket = (Maket) context.getBean("maket");
        System.out.println(maket);
    }

    @Bean
    public static DataSource getDataSource(){
        Properties properties = new Properties();
        properties.setProperty("driver", "com.mysql.jdbc.Driver");
        properties.setProperty("url", "jdbc:mysql://localhost:3306/rwxbase");
        properties.setProperty("username", "root");
        properties.setProperty("password", "admin");
        DataSource dataSource = null;
        try {
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataSource;
    }

}
